package com.rfid.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;

import com.rfid.common.exception.RFIDException;
import com.rfid.common.utils.StringUtils;
import com.rfid.system.domain.TOrderTotal;
import com.rfid.system.domain.TRfidAuditTotal;
import com.rfid.system.service.ITOrderTotalService;
import com.rfid.system.service.common.DictRfidService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rfid.common.annotation.Log;
import com.rfid.common.enums.BusinessType;
import com.rfid.system.domain.TRfidDataDetail;
import com.rfid.system.service.ITRfidDataDetailService;
import com.rfid.common.core.controller.BaseController;
import com.rfid.common.core.domain.AjaxResult;
import com.rfid.common.utils.poi.ExcelUtil;
import com.rfid.common.core.page.TableDataInfo;

/**
 * RFID数据明细Controller
 *
 * @author fangchen
 * @date 2020-12-26
 */
@Controller
@RequestMapping("/system/detail")
public class TRfidDataDetailController extends BaseController {
    private String prefix = "system/detail";

    @Autowired
    private ITRfidDataDetailService tRfidDataDetailService;


    @Autowired
    private DictRfidService dictRfidService;

    @Autowired
    private ITOrderTotalService itOrderTotalService;

    @RequiresPermissions("system:detail:view")
    @GetMapping()
    public String detail() {
        return prefix + "/detail";
    }

    @RequiresPermissions("system:detail:view")
    @GetMapping("/view/{auditId}")
    public String detail(@PathVariable("auditId") Long auditId, ModelMap mmap) {
        TRfidDataDetail tRfidDataDetail = tRfidDataDetailService.selectTRfidDataDetailById(auditId);
        //dictRfidService.getLabelByCode("RFID_FUNCTION",dto.getFuncId()+"")
        String documentNo=tRfidDataDetail.getDocumentNo();
        TOrderTotal tOrderTotal=new TOrderTotal();
        tOrderTotal.setOrderNo(documentNo);
        List<TOrderTotal> orderTotals=itOrderTotalService.selectTOrderTotalList(tOrderTotal);
        if(CollectionUtils.isEmpty(orderTotals)){
            throw new RFIDException("单据号对应的单据信息不存在");
        }
        tOrderTotal=orderTotals.get(0);
        tRfidDataDetail.setOrderTotalDate(tOrderTotal.getOrderDate());
        tRfidDataDetail.setDocumentTypeName(dictRfidService.getLabel("ORDER_TOTAL_TYPE",tOrderTotal.getOrderType()+""));
        mmap.put("tRfidDataDetail", tRfidDataDetail);
        return prefix + "/view";
    }

    /**
     * 查询RFID数据明细列表
     */
    @RequiresPermissions("system:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TRfidDataDetail tRfidDataDetail) {
        startPage();
        List<TRfidDataDetail> list = tRfidDataDetailService.selectTRfidDataDetailList(tRfidDataDetail);
        list.stream().forEach(detail->{
            detail.setFuncName(dictRfidService.getLabelByCode("RFID_FUNCTION",detail.getFuncId()+""));
        });
        if(StringUtils.isNotEmpty(tRfidDataDetail.getProductName())){
            String productName=tRfidDataDetail.getProductName();
            list=list.stream().filter(detail->productName.equals(detail.getProductName())).collect(Collectors.toList());
        }
        if(tRfidDataDetail.getFuncId()!=null){
            String funcName=dictRfidService.getLabelByCode("RFID_FUNCTION",tRfidDataDetail.getFuncId()+"");
            list=list.stream().filter(detail->funcName.equals(detail.getFuncName())).collect(Collectors.toList());
        }
        return getDataTable(list);
    }

    /**
     * 导出RFID数据明细列表
     */
    @RequiresPermissions("system:detail:export")
    @Log(title = "RFID数据明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TRfidDataDetail tRfidDataDetail) {
        List<TRfidDataDetail> list = tRfidDataDetailService.selectTRfidDataDetailList(tRfidDataDetail);
        ExcelUtil<TRfidDataDetail> util = new ExcelUtil<TRfidDataDetail>(TRfidDataDetail.class);
        return util.exportExcel(list, "detail");
    }

    /**
     * 新增RFID数据明细
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存RFID数据明细
     */
    @RequiresPermissions("system:detail:add")
    @Log(title = "RFID数据明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TRfidDataDetail tRfidDataDetail) {
        return toAjax(tRfidDataDetailService.insertTRfidDataDetail(tRfidDataDetail));
    }

    /**
     * 修改RFID数据明细
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TRfidDataDetail tRfidDataDetail = tRfidDataDetailService.selectTRfidDataDetailById(id);
        mmap.put("tRfidDataDetail", tRfidDataDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存RFID数据明细
     */
    @RequiresPermissions("system:detail:edit")
    @Log(title = "RFID数据明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TRfidDataDetail tRfidDataDetail) {
        return toAjax(tRfidDataDetailService.updateTRfidDataDetail(tRfidDataDetail));
    }

    /**
     * 删除RFID数据明细
     */
    @RequiresPermissions("system:detail:remove")
    @Log(title = "RFID数据明细", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tRfidDataDetailService.deleteTRfidDataDetailByIds(ids));
    }
}
